$('.select2').select2();
// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

$(document).ready(function() {
    // $("#product").addClass("active");
    // $("#product5").addClass("active");

    getData(1);
    $('#search_btn').click(function () {
        getData(1);
    });

    function getData(curr) {
        $.ajax({
            type: "get",
            url: "http://114.116.72.220:9999/UseTypeFindProduct",
            async: true,
            data: {
                productGroup1: $("#group1").val(),
                productGroup2: $("#group2").val(),
                page: curr,
                row: 10
            },
            success: function (data) {
                if (data[0].length == 0) {
                    layer.msg('暂无数据');
                }
                pageDivd(curr, data[1]);
                TableView(data[0]);
            }
        });
    }

    function pageDivd(curr, page_num) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'demo7',
                count: page_num,
                limit: 10,
                curr: curr,
                jump: function (obj, first) {
                    if (!first) {
                        getData(obj.curr);

                    }
                }
            });
        });
    }

    function TableView(data) {
        $('.table_data').html('');
        for (var i = 0; i < data.length; i++) {

            var trText = `<tr class="${i % 2 ? '' : 'tr_back'}">
								<td>${i + 1}</td>
		      		    		<td>
		      		    			<img src="${data[i].product_thumbnail}" height="50"/>
		      		    		</td>
		      		    		<td>${data[i].product_no}</td>
		      		    		<td>${data[i].product_name}</td>
		      		    		<!--<td>${data[i][3]}</td>-->
		      		    		<td>
		      		    			￥${data[i].product_price}
		      		    		</td>
		      		 
		      		    		<td index="${data[i].product_id}" style="width: 330px;">
									<button class="look_detail" style="width: 60px">查看</button>
									<button class="kind_del" style="width:144px;margin-left: 44px">此分类下删除商品</button>
								</td>
					</tr>`;

            $('.table_data').append(trText);

        }
        data_edtil();
    }

    function data_edtil() {
        // $('.order_by').click(function (ev) {
        //     var Number = $(this).parent().parent().attr('index');
        //     var ev = ev || event;
        //     $('.check_do').css('display', 'block')
        //     $('.check_do').css('left', ev.clientX + 'px');
        //     $('.check_do').css('top', ev.clientY + 'px');
        //
        //     kindChange(Number);
        // });
        $('.look_detail').click(function () {
            var Index = $(this).parent().attr('index');
            window.location.href = "http://114.116.72.220:9010/src/product_details.html?productId=" + Index;
        });
        $('.kind_del').click(function () {
            var group1 = $("#group1").val();
            var group2 = $("#group2").val();
            var id = $(this).parent().attr('index');
            layer.confirm('确定要在此分类下删除此商品吗？', {
                title: ['提示'],
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type: "get",
                    url: "http://114.116.72.220:9999/DeleteSonProduct",
                    async: true,
                    data: {
                        productId: id,
                        productGroup1: group1,
                        productGroup2: group2
                    },
                    success: function (data) {
                        if (data == 'success') {
                            layer.msg('删除成功', {time: 500}, function () {
                                location.reload()
                            })
                        } else {
                            layer.msg('删除失败', {time: 500})
                        }
                    }
                });
            }, function(){});
        });

    }

    $('#group1').change(function () {
        var man = ['套西', '单西', '裤子', '长袖衬衣', '短袖衬衣', '马甲', '大衣', '风衣', '夹克', '中华立领', '中式西服'];
        var woman = ['套西', '单西', '裤子', '长袖衬衣', '短袖衬衣', '短裙', '大衣', '风衣', '连衣裙', '旗袍', '中式西服'];
        var child = ['套西', '单西', '裤子', '长袖衬衣', '短袖衬衣', '短裙', '大衣', '风衣'];
        var work = ['男士西装', '男士衬衣', '男士裤子', '男士大衣', '男士风衣', '女士西装', '女士衬衣', '女士短裙', '女士大衣', '女士风衣', '连衣裙'];
        var group2 = [];
        var group1 = $("#group1").val();
        if (group1 == "男装") {
            group2 = man;
        } else if (group1 == "女装") {
            group2 = woman;
        } else if (group1 == "童装") {
            group2 = child;
        } else if (group1 == "职业装") {
            group2 = work;
        }

        $("#group2").empty();
        for (var i = 0; i < group2.length; i++) {
            var text = `<option value='${group2[i]}'>${group2[i]}</option>`;
            $("#group2").append(text);
        }
    });

//排序
    function kindChange(num) {
        //互换
        var group1 = $("#kind_sel1").select2('val');
        var group2 = $("#kind_sel2").select2('val');

        $('.order').click(function () {
            var kindLabel = $(this).attr('label');
            $.ajax({
                type: "get",
                url: "https://www.hgxrshn.com/MgProductSorted",
                async: true,
                data: {
                    Id: num,
                    Kind: kindLabel,
                    ProductGroup1: group1,
                    ProductGroup2: group2,
                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('排序成功', {time: 500}, function () {
                            location.reload();
                        })
                    } else {
                        layer.msg('排序失败', {time: 500})
                    }
                }
            });
        });

        $('.order5').click(function () {
            var firseCheck, secondCheck;
            var check = $('input[type=checkbox]:checked');
            if (check.length == 2) {
                firseCheck = check.eq(0).attr('index');
                secondCheck = check.eq(1).attr('index');
                $.ajax({
                    type: "get",
                    url: "https://www.hgxrshn.com/MgProductSwap",
                    async: true,
                    data: {
                        Content: kind_text,
                        Behind: secondCheck,
                        First: firseCheck,

                    },
                    success: function (data) {
                        if (data == 'success') {
                            layer.msg('交换成功', {time: 500}, function () {
                                location.reload();
                            })
                        } else {
                            layer.msg('交换失败', {time: 500})
                        }

                    }
                });
            } else {
                layer.msg('请选中两个商品')
            }
        });
    }

});